﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows;


namespace MyFloridaHouse.libSvOpenDistricts.Census
{
    // Extends CensusGeometry Class to give it ability to import/export Department of Justice files
    public static class CensusGeometryIODOJ
    {
        public static MemoryStream ToDOJ(this CensusGeometry censusGeometry, string ignoreFileName)
        {
            // ignore filename for DOJ format output
            MemoryStream DOJstream = new MemoryStream();
            bool isNotFirst = false;
            foreach (var block in censusGeometry.censusGeometryNodeList)
            {
                // ignore the first node, it represents the highest level geometry node
                if (isNotFirst)
                {
                    // dept. of justice files only care about census blocks
                    if (block.uogType == CensusGeometryConstants.censusBlockUogType)
                    {
                        // don't care about blocks that arent' assigned.
                        if (block.districtID != 0)
                        {
                            string DOJLine = block.doj00 + "," + block.districtID + Environment.NewLine;
                            byte[] data = (new UTF8Encoding()).GetBytes(DOJLine);
                            DOJstream.Write(data, 0, data.Length);
                        }
                    }
                }
                else
                {
                    isNotFirst = true;
                }
            }
            return DOJstream;
        }



        public static bool FromDOJ(this CensusGeometry censusGeometry, StreamReader fileStreamReader, int districtCount)
        {
            bool result = false;
            try
            {
                //// build the districtUogIDList by reading the file and populating the list
                List<DistrictToCensusIDStruct> districtCensusIDList = new List<DistrictToCensusIDStruct>();
                while (!fileStreamReader.EndOfStream)
                {
                    int iDistrictID;
                    string txt = fileStreamReader.ReadLine();
                    string[] values = txt.Split(',');
                    if (values.Length == 2)
                    {
                        string censusID = values[0];
                        Int32.TryParse(values[1], out iDistrictID);
                        districtCensusIDList.Add(new DistrictToCensusIDStruct(iDistrictID, censusID));
                    }
                    else
                    {
                        MessageBox.Show("Error reading the file, can't extract the data from the line [" + txt + "]");
                        break;
                    }
                }

                result = censusGeometry.BypassLoadWithCensusBlocks(districtCensusIDList, districtCount);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error reading the file" + ex.Message);
            }
            return result;

        }

    }
}
